package com.example.finaltool.service;

import com.example.finaltool.mapper.SysUserMapper;
import com.example.finaltool.model.Result;
import com.example.finaltool.model.DepInfo;
import com.example.finaltool.utils.PrintUtils;
import com.example.finaltool.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dengbw
 * @date 2021/8/16 10:34 上午
 */
@Slf4j
@Service
public class SysUserService {
    DingCommonService dingCommonService;
    SysUserMapper mapper;

    @Autowired
    public SysUserService(DingCommonService dingCommonService,
                          SysUserMapper mapper) {
        this.dingCommonService = dingCommonService;
        this.mapper = mapper;
    }

    public Result<List<String>> updateMenu(String appKey, String appSecret) {
        PrintUtils.separator();
        PrintUtils.stepStart("功能更新");
        LocalDateTime start = LocalDateTime.now();
        DepInfo info = dingCommonService.fetchDepInfo(appKey, appSecret);
        List<String> result = new ArrayList<>();

        int updatedFuwu = mapper.updateFuwu(info.getFuwu());
        String res1 = "更新 服务支撑人员功能 " + updatedFuwu + " 条";
        result.add(res1);
        log.info(res1);

        int updatedXiangzhen = mapper.updateNonghu(info.getXiangzhen());
        String res2 = "更新 乡镇管理人员功能 " + updatedXiangzhen + " 条";
        result.add(res2);
        log.info(res2);

        info.getCunDepInfoList().stream().parallel().forEach(c -> {
            int updatedNonghu = mapper.updateNonghu(c.getCunDepId());
            int updatedGanbu = mapper.updateFuwu(c.getCunDepId());
            String res3 = "更新 " + c.getCunName() + " 农户功能 " + (updatedNonghu + updatedGanbu) + " 条";
            result.add(res3);
            log.info(res3);
        });
        PrintUtils.stepFinish("功能更新, 用时" + TimeUtils.duration(start));
        PrintUtils.separator();
        return Result.success(result);
    }

    public int fixMenu(String corpId) {
        return mapper.fixMenu(corpId);
    }
}
